import pgzrun

WIDTH = 480
HEIGHT = 480
BACK_COLOR = (200,75,150)

board = Actor('board')
board.pos = 480 // 2, 480 // 2
space = (2,2)

nums = []
offset = 0
num_size = 160
for i in range(9):
    r = i % 3
    c = i // 3
    num = Actor(str(i + 1))
    num.pos = r * num_size + num_size / 2 + offset, c * num_size + num_size / 2 + offset
    nums.append(num)

def on_mouse_down(pos):
    global space
    i=pos[0]//num_size
    j=pos[1]//num_size
    print(i,j)
    if space in [(j-1,i),(j+1,i),(j,i-1),(j,i+1)]:
        animate(nums[j*3+i], pos=(space[1]*num_size + num_size/2 +offset, space[0]*num_size + num_size/2+offset))
        animate(nums[space[1]*3+space[0]], pos=(j*num_size + num_size/2 +offset, i*num_size + num_size/2+offset))
        space=(i,j)
        nums[j*3+i],nums[space[1]*3+space[0]]=nums[space[1]*3+space[0]],nums[j*3+i]

def update():
    pass

def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    board.draw()
    for n in nums:
        n.draw()

pgzrun.go()